<template>
  <div class="login">
    <van-nav-bar
      title="登录"
      left-arrow
      @click-left="$router.go(-1)"
    />

    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input type="text" placeholder="请输入手机号码" v-model="mobile">
        </div>
        <div class="form-item">
          <input type="text" placeholder="请输入图形验证码" v-model="picCode">
          <img v-if="picUrl" :src="picUrl" alt="验证码" @click="getImgValidCode">
        </div>
        <div class="form-item">
          <input type="text" placeholder="请输入短信验证码" v-model="msgCode">
          <button v-show="countdown <= 0" @click="sendMsgCode">获取验证码</button>
          <span v-show="countdown > 0">重新发送（{{ countdown }}）秒</span>
        </div>
      </div>
      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>

<script>
import { getImgValidCode, loginRequest, sendMsgCode } from '@/api/login'
import { Toast } from 'vant'
import { mapMutations } from 'vuex'

export default {
  name: 'LoginIndex',
  data () {
    return {
      mobile: '',
      picCode: '',
      msgCode: '',
      picUrl: '',
      picKey: '',
      countdown: 0,
      timer: 0
    }
  },
  mixins: [],
  methods: {
    async getImgValidCode () {
      const {
        key,
        base64
      } = await getImgValidCode()
      this.picUrl = base64
      this.picKey = key
    },
    validMobile () {
      if (!/^1[356789]\d{9}$/.test(this.mobile)) {
        Toast.fail('手机号不合法')
        return false
      }
      return true
    },
    sendMsgCodeBeforeValid () {
      if (!this.validMobile()) {
        return false
      }
      if (!/^[a-zA-Z0-9]{4}$/.test(this.picCode)) {
        Toast.fail('图片验证码错误')
        return false
      }
      return true
    },
    async sendMsgCode () {
      if (!this.sendMsgCodeBeforeValid()) {
        return
      }
      await sendMsgCode(this.mobile, this.picKey, this.picCode)
      this.$toast('短信验证码发送成功')
      this.countdown = 5
      this.timer = setInterval(() => {
        if (this.countdown-- <= 0) {
          clearInterval(this.timer)
          this.countdown = 0
        }
      }, 1000)
    },
    async login () {
      if (!this.validMobile()) {
        return false
      }
      const res = await loginRequest(this.mobile, this.msgCode)
      // console.log(res)
      this.$toast('登录成功')
      this.setUserInfo(res)
      const redirectUrl = this.$route.query.redirectUrl || location.origin
      console.log(`redirectUrl = ${redirectUrl}`)
      location.replace(redirectUrl)
    },
    ...mapMutations('user', ['setUserInfo'])
  },
  created () {
    this.getImgValidCode()
  },
  destroyed () {
    clearInterval(this.timer)
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;

    h3 {
      font-size: 26px;
      font-weight: normal;
    }

    p {
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;

    input {
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }

    img {
      width: 94px;
      height: 32px;
    }

    button {
      height: 32px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background: transparent;
    }

    span {
      font-size: 13px;
      color: #b8b8b8;
    }
  }

  .login-btn {
    width: 100%;
    //height: 42px;
    margin-top: 39px;
    border-radius: 20px;
    background: linear-gradient(to right, #ecb53c, #ff9211);
    text-align: center;
    line-height: 42px;
    color: #fff;
    box-shadow: 0 10px 20px 0 rgba(0, 0, 0, .1);
  }
}
</style>
